package com.houning.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import com.houning.config.ServerProperties;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/template")
@RequiredArgsConstructor
public class TemplateController {
    private final ResourceLoader resourceLoader;
    private final ServerProperties serverProperties;

    private final DocumentConverter converter;

    @SneakyThrows
    @GetMapping("/export")
    public void export(@RequestParam Integer count, HttpServletResponse response) {

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline; filename=\"out.pdf\"");

        Map<String, Object> tablesMap = new HashMap<>();
        int tableCount = count;
        List<Map<String, Object>> tables = new ArrayList<>();
        for (int i = 0; i < tableCount; i++) {
            Map<String, Object> contentMap = new HashMap<>();
            contentMap.put("title", "表格: " + (i + 1));
            tables.add(contentMap);

            RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
                    .bgColor("4472C4").center().create();
            RowRenderData row1 = Rows.create("李四", "博士");
            contentMap.put("content", Tables.create(row0, row1));
        }
        tablesMap.put("tables", tables);
        try (InputStream inputStream = readTemplate();
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
        ) {
            XWPFTemplate template = XWPFTemplate.compile(inputStream).render(tablesMap);
            template.writeAndClose(outputStream);
            byte[] byteArray = outputStream.toByteArray();

            try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {


                converter.convert(bais).as(DefaultDocumentFormatRegistry.DOCX)
                        .to(response.getOutputStream())
                        .as(DefaultDocumentFormatRegistry.PDF).execute();


            }

//            template.writeAndClose(new FileOutputStream("output.docx"));

            System.out.println("Generating " + tableCount + " tables successful!");
        }

    }
    @SneakyThrows
    public InputStream readTemplate() {
        Resource resource = resourceLoader.getResource(serverProperties.getTemplatePath());
        return resource.getInputStream();
    }
}
